const fs = require("fs");
const path = require('path');

const root = path.resolve(__dirname, '..', '..', '..')
const pkg_path = path.join(root, 'package.json')

const shell = require("shelljs");
const pkg = require(pkg_path);
const pk_version = pkg.version;
const prev = pkg['pre-version'];

// const getCommitMsg = () => fs.readFileSync(".git/COMMIT_EDITMSG", "utf8");
// const message = getCommitMsg();

function preVersion(){
    let newVersion = '';
    if(prev) {
        newVersion = prev
    }else{
        const temp = pk_version.split('.');
        let patch = +temp[2] + 1;
        if(patch>=100) {
            patch = 0;
            temp[1] = +temp[1] + 1;
        };
        temp[2] = patch;
        newVersion = temp.join('.');
    };

    const newPkg = Object.assign({}, pkg, { version: newVersion }); // update version

    fs.writeFileSync(pkg_path, JSON.stringify(newPkg, null, 2));
    shell.exec(`git add ${pkg_path}`);

    console.log('版本补全',newVersion);
}

module.exports = preVersion;
